package com.example.ole.aspectjbase.aop;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;

import com.example.ole.aspectjbase.A;
import com.example.ole.aspectjbase.annotation.PermissionDenied;
import com.example.ole.aspectjbase.annotation.PermissionRequired;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

@Aspect
public class PermissionAspect {
    public static final String TAG = "PermissionAspect";
    private static final String POINTCUT_METHOD =
            "execution(@com.example.ole.aspectjbase.annotation.PermissionRequired * *(..))";

    @Pointcut(POINTCUT_METHOD + " && @annotation(permissions)")
    public void pointCut(PermissionRequired permissions) {
    }

    @Around("pointCut(permissions)")
    public void weaveJoinPoint(final ProceedingJoinPoint jp, final PermissionRequired permissions) throws Throwable {
        final String cls = jp.getSignature().getDeclaringType().getSimpleName();
        String mth = jp.getSignature().getName();

        Log.w(TAG, cls + "::" + mth + " request permissions: "
                + Arrays.toString(permissions.required()));

        Context context = null;
        final Object object = jp.getThis();

        if (object instanceof Context) {
            context = (Context) object;
        } else if (object instanceof Fragment) {
            context = ((Fragment) object).getActivity();
        } else if (object instanceof android.support.v4.app.Fragment) {
            context = ((android.support.v4.app.Fragment) object).getActivity();
        } else if (object instanceof A) {
            context = (Context) jp.getArgs()[0];
        }

        if (context == null || permissions == null)
            return;

//        final ArrayList<String> mGranted = new ArrayList<>();
//        final ArrayList<String> mCanceled = new ArrayList<>();
//        final ArrayList<String> mDenied = new ArrayList<>();
//
//        for (final String permission : permissions.required()) {
//            int granted = ContextCompat.checkSelfPermission(context, permission);
//            if (PackageManager.PERMISSION_GRANTED != granted) {
//                if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permission)) {
//                    final Context finalContext = context;
//                    AlertDialog dialog = new AlertDialog.Builder(context).setTitle(permission)
//                            .setPositiveButton("Grant!", new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialog, int which) {
//                                    mGranted.add(permission);
//                                }
//                            }).setNegativeButton("Deny", new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialog, int which) {
//                                    mDenied.add(permission);
//                                }
//                            }).show();
//                    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
//                        @Override
//                        public void onCancel(DialogInterface dialog) {
//                            mCanceled.add(permission);
//                        }
//                    });
//                }
//            } else {
//                mGranted.add(permission);
//            }
//        }

        ActivityCompat.requestPermissions((Activity) context,
                permissions.required(),
                permissions.requestCode());
    }
}
